
// 整理
const grant_type = 'client_credentials'
const client_id = ''
const client_secret = ''
var token = null
var base64 = null
var apiUrl = null


Page({
  data: {
    imageUrl: "/images/upimage.png",
    load_logo: 'waiting',
    load_title: [],
    load_message: " 请上传图片",
  },

  onReady: function(res) {
    // get access_token from BaiDu API
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=' + grant_type + '&client_id=' + client_id + '&client_secret=' + client_secret,
      method: 'POST',
      success: function (res) {
        // console.log('Request successful !')
        // console.log(res.data)
        token = res.data.access_token;
        // console.log('My token is : ' + token);
      },
      fail: function (res) {
        // console.log('Fail to request !')
        // console.log(res)
      }
    })
  },

  get_image: function(res) {
    var that = this
    that.setData({
      load_title: [],
      load_message: "正在上传图片,请稍后"      
    })
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths
        apiUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/search'
        that.setData({
          imageUrl: tempFilePaths,
          load_logo: "success",
          load_message: "等待识别图片,请点击识别按钮以识别图片",
          load_title: [],
          btn_enable: false
        })
        // console.log('My API URL is : ' + apiUrl)
        // console.log('Image Path is : ' + tempFilePaths)
        // console.log(res)
        wx.getFileSystemManager().readFile ({
          filePath: res.tempFilePaths[0],
          encoding: 'base64',
          // complete: res=> {
          //   console.log('complete')
          //   console.log(res)
          // },
          success: res => {
            base64 = res.data
            // console.log('data:image/png;base64,' + base64)
          }
        })
      }
    })
  },

  recognition_image: function(res) {
    var that = this
    that.setData({
      load_title: [],
      load_message: "正在识别图片,请稍后"
    })
    wx.request({
      url: apiUrl + '?access_token=' + token,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        image: base64
      },
      success: res => {
        var result = null
        // console.log('recognition_image Success')
        if(res.data.result == null) {
        //   console.log(res.data.error_msg)
        //   console.log(base64)
          result = res.data.error_msg
        }else {
          console.log(res.data.result)
          result = res.data.result
        }
        that.setData({
          load_title: result,
        })
      }
    })
  },

      // 点击进行详情页跳转
      imgshow: function(e) {
        // 拿到当前图片的数据并且整理数据'
        console.log(e.currentTarget.dataset.index)
        const index = e.currentTarget.dataset.index
        const load_title = this.data.load_title
        console.log(load_title[index].brief)
        // 整理数据
        const dataobj = {
            url: load_title[index].brief
        }
        



        // 跳转页面
        wx.navigateTo({
          url: '../detail2/detail2',
          success: (result) => {
              result.eventChannel.emit('acceptDataFromOpenerPage',{ data: dataobj})
          },

        })
    }
})